package com.neuedu.dao.impl;

import com.neuedu.common.DBUtil;
import com.neuedu.dao.BaseDao;
import com.neuedu.dao.UserDao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

public class UserDaoImpl extends BaseDao implements UserDao {
    @Override
    public int updateUser(Object[] objs) {
        String sql = "UPDATE `user` SET loginname=?,`name`=?,pass=?,deptid=(SELECT id FROM deptment WHERE deptname=?),usercategoryid=(SELECT id FROM usercategory WHERE category=?),doctorpositionid=(SELECT id FROM doctorposition WHERE position=?),registeredlevelid=(SELECT id FROM registeredlevel WHERE `level`=?),isscheduling=?  WHERE id=?";
        Connection con = DBUtil.getCon();
        int flag = 0;
        try {
            flag = this.delUpins(sql, objs, con);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }

    @Override
    public int getCount() {
        String sql = "select count(*) from `user`";
        Connection con = DBUtil.getCon();
        int count = -1;
        try {
            ResultSet rs = con.prepareStatement(sql).executeQuery();
            if (rs.next()) {
                count = rs.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return count;
    }

    @Override
    public List<Map<String, Object>> getUser(Object[] objs) {
        String sql = "SELECT u.id,u.pass,u.loginname,u.`name`,uc.category,d.position,dt.deptname,rl.`level`,u.isscheduling FROM `user` u,doctorposition d,usercategory uc,registeredlevel rl,deptment dt WHERE u.doctorpositionid=d.id AND u.usercategoryid=uc.id AND u.registeredlevelid=rl.id AND u.deptid=dt.id limit ?,?";
        Connection con = DBUtil.getCon();
        List<Map<String, Object>> list = null;
        int[] parm1 = new int[objs.length];
        for (int i = 0; i < objs.length; i++) {
            parm1[i] = (int) objs[i];
        }
        try {
            list = this.queryParmSqlAsName(sql, parm1, con);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public int deleteUser(Object[] objs) {
        String sql = "DELETE FROM `user` WHERE id=?";
        Connection con = DBUtil.getCon();
        int flag = 0;
        try {
            flag = this.delUpins(sql, objs, con);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }

    @Override
    public int addUser(Object[] objs) {
        String sql = "INSERT INTO `user`(loginname,name,pass,deptid,usercategoryid,doctorpositionid,registeredlevelid,isscheduling) VALUES(?,?,?,?,?,?,?,?)";
        Connection con = DBUtil.getCon();
        int flag = 0;
        try {
            flag = this.delUpins(sql, objs, con);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return flag;
    }

    @Override
    public List<Map<String, Object>> getLikeUser(String sql, Object[] objs) {
        Connection con = DBUtil.getCon();
        List<Map<String, Object>> list = null;
        try {
            list = this.queryParmSqlAsName(sql, objs, con);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }

    @Override
    public List<Map<String, Object>> userGetByDept(Object[] objs) {
        String sql = "select * from `user` where deptid=? limit ?,?";
        Connection con = DBUtil.getCon();
        List<Map<String, Object>> list = null;
        int[] parm1 = new int[objs.length];
        for (int i = 0; i < objs.length; i++) {
            parm1[i] = (int) objs[i];
        }
        try {
            list = this.queryParmSqlAsName(sql, parm1, con);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return list;
    }
}
